import type { FinAccountApi } from '.'
import type { ClassInfoResp } from '../education/classInfo'
import http from '@/utils/http'

const BASE_URL = '/shop/order'

export interface Order {
  id: string
  studentId: string
  originalTotalPrice: number
  actualTotalPrice: number
  discountDetail: Record<string, any>
  status: string
  payOff: boolean
  payType?: string
  payTime?: string
  outTradeNo?: string
  payOrderDetail?: string
  finAccountId?: string
  cancelTime?: string
  // refunded: boolean
  refundPrice?: number
  remark?: string
  remarkOut?: string
  endValidTime: string
  // sellerId?: string
  operateSource?: string
}

export interface OrderQuery {
  orderId?: string
  payType?: string
  finAccountId?: string
  // refunded?: boolean
  // canceled?: boolean
  // payOff?: boolean
  // classId?: string
  status?: string[]
  studentId?: string
  createDateFrom?: string
  createDateTo?: string
  payDateFrom?: string
  payDateTo?: string
}

export interface OrderResp extends Order {
  orderItemList: {
    className: string
    lessonIndexes: string
  }[]
  student: any
  finAccount: FinAccountApi.FinAccountResp
}

export interface OrderPriceDetail {
  originalTotalPrice: number
  actualTotalPrice: number
  discountTotalPrice: number
  couponDiscountPrice: number
  packageDiscountPrice: number
  otherDiscountPrice: number
}

export interface OrderStatResp {
  todayOrderCount: number
  pendingPayCount: number
  totalOrderAmount: number
  totalOrderCount: number
}

export interface OrderClass extends ClassInfoResp {
  lessonList: any[]
}

export interface OrderItem {
  id: string
  orderId: string
  classId: string
  studentId: string
  className: string
  lessonIndexes: string
  lessonIndexList: string[]
  actualTotalPrice: number
  actualUnitPrice: number
}

export interface OrderDetailResp extends OrderResp {
  orderItemList: OrderItem[]
}

export interface OrderCreateReq {
  studentId?: string
  orderItemList?: any[]
  couponIdList?: string[]
  remark?: string
  remarkOut?: string
  payType: string
  sellerId?: string
}

export interface OrderEndValidTimeUpdateReq {
  orderId: string
  endValidTime: string
}

export interface OrderPayTypeUpdateReq {
  orderId?: string
  finAccountId?: string
  payTime?: string
}

export interface OfflinePayReq {
  orderId?: string
  payAmount?: number
  finAccountId?: string
  remark?: string
}

export interface OrderRecentStatQuery {
  dateFrom: string
  dateTo: string
}

export interface OrderRecentStatResp {
  orderCountStat: Record<string, number>
  orderPriceStat: Record<string, number>
}

export const calculatePrice = (any) => {
  return http.post<OrderPriceDetail>(`${BASE_URL}/price`, any)
}

export const createOrder = (data: OrderCreateReq) => {
  return http.post(`${BASE_URL}/create`, data)
}

export const getOrderPage = (query: OrderQuery & PageQuery) => {
  return http.get<Page<OrderResp[]>>(`${BASE_URL}/page`, query)
}

export const getOrderStat = (query: OrderQuery) => {
  return http.get<OrderStatResp>(`${BASE_URL}/stat`, query)
}

export const getOrderRecentStat = (query: OrderRecentStatQuery) => {
  return http.get<OrderRecentStatResp>(`${BASE_URL}/stat/recent`, query)
}

export const removeOrder = (id: string | number) => {
  return http.post(`${BASE_URL}/delete/${id}`)
}

export const getOrderDetail = (id: string) => {
  return http.get<OrderDetailResp>(`${BASE_URL}/detail/${id}`)
}

export const cancelOrder = (id: string) => {
  return http.post(`${BASE_URL}/cancel/${id}`)
}

export const offlinePay = (req: OfflinePayReq) => {
  return http.post('/shop/pay/offline', req)
}

export const refreshPayResult = (orderId: string) => {
  return http.post(`/shop/pay/result/refresh?orderId=${orderId}`)
}

export const updateEndValidTime = (req: OrderEndValidTimeUpdateReq) => {
  return http.post(`${BASE_URL}/updateEndValidTime`, req)
}

export const updatePayType = (req: OrderPayTypeUpdateReq) => {
  return http.post(`${BASE_URL}/updatePayType`, req)
}

export const downloadOrderList = () => {
  return http.download(`${BASE_URL}/downloadOrderList`)
}
